//
//  UIBarButtonItem+Extension.swift
//  WeiBoSwift
//
//  Created by rong on 15/7/1.
//  Copyright © 2015年 rong. All rights reserved.
//

import UIKit

/*
    //1.设置"左右"按钮
    //导航栏 左边的
    let leftBtn = UIButton()
    //尺寸一定要设置
    leftBtn.frame = CGRectMake(20,20, 30, 30)
    leftBtn.setImage(UIImage(named: "navigationbar_friendsearch"), forState: UIControlState.Normal)
    leftBtn.setImage(UIImage(named: "navigationbar_friendsearch_highlighted"), forState: UIControlState.Highlighted)
    navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftBtn)
*/


extension UIBarButtonItem {

    convenience init(imageName: String, highlightedImageName: String?) {
        
        let btn = UIButton()
        
        //尺寸一定要设置
        //btn.frame = CGRectMake(20,20, 30, 30)
        
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        
        let hImageName = highlightedImageName ?? imageName + "_highlighted"
        
        btn.setImage(UIImage(named: hImageName), forState: UIControlState.Highlighted)

        btn.sizeToFit()
        
        self.init(customView: btn)
    }
    
    convenience init(imageName: String, highlightedImageName: String?, target: AnyObject?, actionName: String?) {
        
        let btn = UIButton()
        
        //尺寸一定要设置
        //btn.frame = CGRectMake(20,20, 30, 30)
        
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        
        let hImageName = highlightedImageName ?? imageName + "_highlighted"
        
        btn.setImage(UIImage(named: hImageName), forState: UIControlState.Highlighted)
        
        btn.sizeToFit()
        
        //监听方法
        if actionName != nil {
            btn.addTarget(target, action: Selector(actionName!), forControlEvents: UIControlEvents.TouchUpInside)
        }
        
        self.init(customView: btn)
    }


}